#!/bin/bash

# 备份脚本到主库
cp $0 Specs/

if [ $# -lt 2 ]
then
    echo 'argument too less then 2'
    echo "\033[31;1mUse './update.sh GitDir VERSION'.\033[0m"
    exit
fi

tag_num=$2
last_tag=""
privateSpecName='lsp'


cd $1

tags=(`git tag`)

echo -e "\033[33;1m=================> UPDATE START >>>\033[0m"


for file in *.podspec
do
#    修正版本号 并提交到git
    sed -i "" "s/\(.*\)\(.version\ \{0,\}\)\(=.*\)\('.*'\)/\1\2\3'$tag_num'/g" $file
    
    #提交本地更改
    echo 'commiting and push any changes to remote.'
    echo -e "\033[35;1mgit commit -a -m \"tag $tag_num\"\033[0m"
    git commit -a -m "tag $tag_num"
    #推送到远端
    echo -e "\033[35;1mgit push -u origin\033[0m"
    git push -u origin
    #远端更新完成
    echo -e "\033[33;1mthe remote up-to-date.\033[0m"
    
    if [ ${#tags[@]} == 0 ]
    then
        echo -e "\033[33;1mgit has no tags.\033[0m"
    else
        echo -e "\033[35;1m\033[0m"
        tagExist=false
        for ele in ${tags[@]}
        do
            if [[ $tag == $ele ]]; then
                tagExist=true
                break
            fi
        done

    
        if [[ "$tagExist" = true ]] ; then
            echo -e "\033[33;1mtag $tag_num exists.\033[0m"
            
            echo -e "\033[33;1mpod spec lint --sources=https://gitee.com/1210233/SPSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings\033[0m"
            echo -e "\033[33;1mpod repo push $privateSpecName $file --allow-warnings\033[0m"
            echo -e "\033[31;1mThis commands may be help for you.\033[0m"
            exit
        fi
    fi
    
    echo "now create the new tag $tag_num and push to remote."

    #本地创建tag
    echo -e "\033[35;1mgit tag -m \"$tag_num\" $tag_num\033[0m"
    git tag -m "$tag_num" $tag_num
#    exists_flag="already exists"
    echo -e "\033[33;1mgit has create the new tag $tag_num.\033[0m"
    
    #推送tag到远端
    echo -e "\033[35;1mgit push origin $tag_num\033[0m"
    pushRR=$(git push origin $tag_num)
#    echo "$pushRR"

#    success_flag="[new tag]"
#    #检查tag推送结果
#    if [[ $pushRR =~ $success_flag ]]
#    then
#        # tag推送成功
#        echo -e "\033[33;1mgit has create the new tag $tag_num.\033[0m"
#    elif [[ "$pushRR" =~ "already exists" ]]
#    then
#        echo -e "\033[31;1mtag already exists on remote.\033[0m"
#        exit
#    else
#    echo "$pushRR"
#        echo -e "\033[31;1mtag $tag_num push faild.\033[0m"
#        exit
#    fi
#
#    exit
        
        
    if [ ${#tags[@]} != 0 ]
    then
        last_tag=${tags[@]: -1}
        echo -e "\033[33;1mand last tag is $last_tag.\033[0m"
    fi
    
    
#    if [ "$last_tag" != "" ]
#    then
#        echo "perform replace spec version in podspec file with the code,"
#        echo "sed -i '.bak' 's/$last_tag/$tag_num/g' $file"
#        sed -i ".bak" "s/$last_tag/$tag_num/g" $file
#    fi



    # Pod spec lint start
    echo -e "\033[35;1mpod spec lint --sources=https://gitee.com/1210233/SPSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings\033[0m"
    rr=$(pod spec lint --sources=https://gitee.com/1210233/SPSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings)

    success_flag="$file passed validation"

    if [[ $rr =~ $success_flag ]]
    then
        echo -e "\033[33;1mpod spec lint success.\033[0m"

        # Pod repo push start.
        echo -e "\033[33;1mpod repo push $privateSpecName $file --allow-warnings\033[0m"
        rr=$(pod repo push $privateSpecName $file --allow-warnings)

        if [[ $rr =~ "Adding the spec to the " &&  $rr =~ "Pushing the " ]]
        then
            echo -e "\033[33;1mpod push spec to '$privateSpecName' success.\033[0m"

#            echo "now excuting 'pod repo update $privateSpecName'..."
#            pod repo update $privateSpecName
            echo -e "\033[32;1m>>>>>>>>>>>>>>>>=== UPDATE END =\033[0m"
        else
            echo $rr
            echo -e "\033[31;1mpod push spec to '$privateSpecName' faild.\033[0m"
        fi
        # Pod repo push end.

    else
        echo $rr
        echo -e "now \033[33mrollback the remote\033[0m with '\033[35;1mgit tag -d $tag_num\033[0m && \033[35;1mgit push origin :refs/tags/$tag_num\033[0m'."
        git tag -d $tag_num
        git push origin :refs/tags/$tag_num
        echo -e "\033[31;1mpod spec $file lint faild.\033[0m"
    fi
    # Pod spec lint end

    break

#    执行一次直接结束
    exit
    
##    for repo in `pod repo list | grep -Ev '^$|^-'`
##    do
##    done
done

exit
